package org.xxpay.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.exception.ServiceException;
import org.xxpay.core.entity.TbWang;
import org.xxpay.core.entity.TbWangExample;
import org.xxpay.core.service.ITbWangService;
import org.xxpay.service.dao.mapper.TbWangMapper;

import java.util.Date;
import java.util.List;

/**
 * @author: pangxiaoyu
 * @date: 2020/02/10
 * @description: 淘宝旺信
 */
@Service
public class TbWngServiceImpl implements ITbWangService {

    @Autowired
    private TbWangMapper tbWangMapper;

    @Override
    public int add(TbWang tbWang) {
        return tbWangMapper.insert(tbWang);
    }

    @Override
    public int update(TbWang tbWang) {
        return tbWangMapper.updateByPrimaryKeySelective(tbWang);
    }

    @Override
    public List<TbWang> select(int offset, int limit, TbWang tbWang, Date createTimeStart, Date createTimeEnd) {
        TbWangExample example = new TbWangExample();
        example.setOffset(offset);
        example.setLimit(limit);
        TbWangExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, tbWang, createTimeStart, createTimeEnd);
        example.setOrderByClause("createTime DESC");
        return tbWangMapper.selectByExample(example);
    }

    @Override
    public Integer count(TbWang tbWang, Date createTimeStart, Date createTimeEnd) {
        TbWangExample example = new TbWangExample();
        TbWangExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, tbWang, createTimeStart, createTimeEnd);
        return tbWangMapper.countByExample(example);
    }

    @Override
    public int updateByDeviceNo(String deviceNo, byte status) {
        if (StringUtils.isEmpty(deviceNo)) throw ServiceException.build(RetEnum.RET_SERVICE_DEVICENO_NULL);
        TbWang tbWang = new TbWang();
        tbWang.setDeviceNo(deviceNo);
        tbWang.setStatus(status);
        return tbWangMapper.updateByDeviceNo(tbWang);
    }

    @Override
    public TbWang getTbWangByOrderNo(String orderNo) {
        return tbWangMapper.selectByPrimaryKey(orderNo);
    }

    @Override
    public TbWang getTbWangByAmount(String payOrderId, Long money, String deviceNo) {
        if (StringUtils.isEmpty(payOrderId)) throw ServiceException.build(RetEnum.RET_SERVICE_PAYORDER_ID_NULL);
        if (money == null) throw ServiceException.build(RetEnum.RET_SERVICE_MONEY_NULL);

        TbWang tbWang = new TbWang();
        tbWang.setStatus(MchConstant.TB_WANG_ORDER_STATUS_CREATE);
        tbWang.setDeviceNo(deviceNo);
        tbWang.setMoney(money);
        TbWang tb = tbWangMapper.getTbWangByAmount(tbWang);

        if (tb != null) {
            tb.setPayOrderId(payOrderId);
            tb.setStatus(MchConstant.TB_WANG_ORDER_STATUS_USED);
            int update = tbWangMapper.updateStatus(tb);
            if (update == 1) return tb;
        }
        return null;
    }

    @Override
    public TbWang getTbWangByPayOrderId(String payOrderId) {
        TbWangExample example = new TbWangExample();
        TbWangExample.Criteria criteria = example.createCriteria();
        criteria.andPayOrderIdEqualTo(payOrderId);
        example.setOrderByClause("createTime DESC");
        List<TbWang> wangList = tbWangMapper.selectByExample(example);
        if (wangList.size() == 0) return  null;
        return wangList.get(0);
    }

    @Override
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public int batchInsertTbWang(List<TbWang> list) {
        if (list.isEmpty()) throw ServiceException.build(RetEnum.RET_SERVICE_TBWANG_LIST_NULL);
        int count = 0;
        for (TbWang tb: list) {
            int insert = tbWangMapper.insert(tb);
            if (insert != 0) count ++;
        }
        return count;
    }

    void setCriteria(TbWangExample.Criteria criteria, TbWang tbWang, Date createTimeStart, Date createTimeEnd) {
        if(tbWang != null) {
            if(StringUtils.isNotEmpty(tbWang.getOrderNo())) criteria.andOrderNoEqualTo(tbWang.getOrderNo());
            if(StringUtils.isNotEmpty(tbWang.getPayOrderId())) criteria.andPayOrderIdEqualTo(tbWang.getPayOrderId());
            if(StringUtils.isNotEmpty(tbWang.getQunId())) criteria.andQunIdEqualTo(tbWang.getQunId());
            if(StringUtils.isNotEmpty(tbWang.getDeviceNo())) criteria.andDeviceNoEqualTo(tbWang.getDeviceNo());
            if(StringUtils.isNotEmpty(tbWang.getRemark())) criteria.andRemarkEqualTo(tbWang.getRemark());
            if(tbWang.getStatus() != null && tbWang.getStatus() != -99) criteria.andStatusEqualTo(tbWang.getStatus());
            if(tbWang.getMoney() != null && tbWang.getMoney() != 0) criteria.andMoneyEqualTo(tbWang.getMoney());
        }
        if(createTimeStart != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(createTimeStart);
        }
        if(createTimeEnd != null) {
            criteria.andCreateTimeLessThanOrEqualTo(createTimeEnd);
        }
    }
}
